/**
 * Created by IntelliJ IDEA.
 * User: kaireewu
 * Date: 11-10-19
 * Time: 下午3:30
 * To change this template use File | Settings | File Templates.
 */
$(document).ready(function(){
    $(".link-edit-item").click(function(event){
        e = event || window.event;
        var sid = $(this).attr('data-id');
        $.ajax({
            type: 'POST',
            url:  'ajaxapi.php',
            data: 'action=getItem&sid=' + sid,
            context:this,
            dataType: 'json',
            success:  showEdit,
            complete: function(xhr, textStatus){
                console.log(textStatus);
            }
        });
        e.preventDefault();
        return false;
    });
    $("#btn-reg, #btn-login, #btn-edit").click(function(e){
        if (!e) {
            e = window.event;
        }
        var key = $(this).attr('id');
        if(key == 'btn-edit' && undefined == user) {
        } else {
            setPop($(this).attr('id'));
        }
    	e.preventDefault();
    	return false;
    });

    $(".pop-common .close").click(function(e){
        if(!e)
            e = window.event;
        $(this).closest(".pop-common").slideUp();
        e.preventDefault();
        return false;
    });
    $("#addForm").submit(function(e){
        if(!e){
            e = window.event;
        }
        var postData = $(this).serialize();
        $.ajax({
            type: 'POST',
            url: 'ajaxapi.php',
            data: 'action=dopost&' + postData,
            context:this,
            dataType: 'json',
            success: postHandler,
            complete: function(xhr, textStatus){
                console.log(textStatus);
            }
        });
        e.preventDefault();
        return false;
    });
    $("#loginForm").submit(function(e){
        if (!e){
            e = window.event;
        }
        var postData = $(this).serialize();
        $.ajax({
            type: 'POST',
            url:  'ajaxapi.php',
            data: 'action=login&' + postData,
            context:this,
            dataType: 'json',
            success:  loginHandler,
            complete: function(xhr, textStatus){
                console.log(textStatus);
            }
        });
        e.preventDefault();
        return false;
    });
    $("#regForm").submit(function(e){
        if (!e) {
            e = window.event;
        }
        var postData = $(this).serialize();
        $.ajax({
            type: 'POST',
            url:  'ajaxapi.php',
            data: 'action=reg&' + postData,
            context: this,
            dataType: 'json',
            success: loginHandler,
            complete: function(xhr, textStatus) {
                console.log(textStatus);
            }
        });
        e.preventDefault();
        return false;
    });
    $("#editForm").submit(function(e){
        if (!e) {
            e = window.event;
        }
        var postData = $(this).serialize();
        $.ajax({
            type: 'POST',
            url: 'ajaxapi.php',
            data: 'action=edit&' + postData,
            context: this,
            dataType: 'json',
            success: loginHandler,
            complete:function(xhr, textStatus) {
                console.log(textStatus);
            }
        });
        e.preventDefault();
        return false;
    });
});
function setSId(sid) {
    document.getElementById('hiddenSId').setattribute("value", sid);
}
function setPop(key) {
    var keyPop = '';
    switch (key) {
        case 'btn-login':
            keyPop = 'pop-login';
            break;
        case 'btn-reg':
            keyPop = 'pop-reg';
            break;
        case 'btn-edit':
            keyPop = 'pop-edit';
            break;
        default:
            keyPop = false;
            break;
    }
    if(keyPop){
        var cssAttr = {'top':$('.header').outerHeight() + 10, 'left':$(document).width() - $('#'+keyPop).width() - 15};
        var obj = $(".pop-common:visible");
        if (obj.length > 0) {
            obj.fadeOut(function(){
                $("#"+keyPop).css(cssAttr).slideDown();
            });
        } else {
            $("#"+keyPop).css(cssAttr).slideDown();
        }
    }
}
function loginHandler(data){
    if(data.code){
        data.msg = data.msg.replace("\\n", "\n");
        alert(data.msg);
    } else {
        location.reload();
    }
}

function showEdit(data){
    if(data.code) {
        data.msg = data.msg.replace("\\n", "\n");
        alert(data.msg);
    } else {
        $('#addTitle').val(data.body.title);
        $('#addUrl').val(data.body.url);
        $("#addCategory").attr("value","Normal");
        $("#addCategory").val("Normal");
        $("#addCategory").get(0).value = data.body.cid;
        $("#addContent").val(data.body.intro);
        $("#hiddenSId").val(data.body.sid);
        $("#addContent").focus();
    }
}
function postHandler(data){
    if(data.code){
        $(".result-msg", this).text(data.msg);
    }else{
        location.reload();
    }
}